
package com.cortex.carambola.view;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.view.View.OnClickListener;

import com.cortex.carambola.listener.SlidView;
import com.cortex.carambola.listener.SlidView.OnSlidFingerListener;
import com.cortex.carambola.listener.SlidView.SlidDirection;
import com.cortex.carambola.utilities.Utilities;
import com.cortex.carambolasummary.R;

public class CortexRowSearch extends EditText 
implements OnEditorActionListener,OnSlidFingerListener {

	private ISlidFinger slidFinger;
	private boolean isCaughtView = false;
	public CortexRowSearch(Context context) {
		super(context);
	}
	public CortexRowSearch(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CortexRowSearch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		init();
		initHandlers();
	}
	private void init(){
		Typeface tf = Typeface.createFromAsset(getContext().getAssets(),"fonts/robotoLight.ttf");
		setTypeface(tf,Typeface.NORMAL);
	}
	private void initHandlers(){
		setOnEditorActionListener(this);
		setOnTouchListener(new SlidView(3, this, SlidDirection.RIGHT,SlidDirection.LEFT));
	}
	public boolean isEnteredObject(){
		return isCaughtView;
	}
	public void setOnSlidFingerListener(ISlidFinger slidFinger){
		this.slidFinger = slidFinger;
	}
	@Override
	public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
		outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
		return super.onCreateInputConnection(outAttrs);
	}
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		slidFinger(true);
		return true;
	}
	@Override
	public void startedSlid(View v) {
		this.setFocusable(true);
		requestFocus();
	}
	@Override
	public void finishedSlid(View v,SlidDirection slidDirection) {
		if(slidDirection == SlidDirection.RIGHT){
			slidFinger(true);
		}
		else if(slidDirection == SlidDirection.LEFT){
			slidFinger(false);
		}
	}
	public String getValue() {
		return super.getText().toString();
	}
	private void slidFinger( boolean isRightSlid){
		if(slidFinger != null){
			if(isRightSlid){
				if(getValue().isEmpty()){
					setHint(R.string.writeSomethingHere);
				}
				slidFinger.toRight();
			}
			else{
				clear();
				slidFinger.toLeft();
			}
		}
		// when user slid to right or press on search in keyboard hide keyboard
		// when user slid to left open keyboard
		Utilities.showKeyboard(!isRightSlid,this);
	}
	public void clear() {
		this.setText("");
	}
	public interface ISlidFinger{
		void toRight();
		void toLeft();
	}
}
